package LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LC_039_CombinationSum {
    public static void main(String[] args) {

    }

    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        Arrays.sort(candidates);
        findSum(candidates, target, 0, 0, temp, res);
        return res;
    }

    private void findSum(int[] candidates, int target, int sum, int level, List<Integer> temp, List<List<Integer>> res) {
        if (sum == target) {
            res.add(new ArrayList<>(temp));
        } else if (sum < target) {
            for (int i = level; i < candidates.length; i++) {
                temp.add(candidates[i]);
                findSum(candidates, target, sum + candidates[i], i, temp, res);
                temp.remove(temp.size() - 1);
            }
        }
    }

}
